package com.ed.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ed.domain.ChatConversation;
import com.ed.domain.Response;
import com.ed.domain.User;
import com.ed.mapper.*;
import com.ed.service.DataScreenService;
import com.ed.utils.TimeUtil;
import com.ed.vo.DataScreenVo;
import com.ed.vo.LatestActivityVo;
import com.ed.vo.UserChatMessageVo;
import com.ed.vo.UserResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class DataScreenServiceImpl implements DataScreenService {
    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private IResponseMapper responseMapper;
    @Autowired
    private IResponseDetailsMapper responseDetailsMapper;
    @Autowired
    private IChatConversationMapper conversationMapper;
    @Autowired
    private IChatMessageMapper chatMessageMapper;
    @Override
    public DataScreenVo listData() {
        //1.用户总数
        DataScreenVo res = new DataScreenVo();
        Long users = userMapper.selectCount(new QueryWrapper<>());
        res.setUserNums(users.intValue());
        //2.昨日活跃人数
        //2.1昨日答题提交的人
        Set<Integer> userSet = new HashSet<>();  //去重
        QueryWrapper<Response> wrapper = new QueryWrapper<>();
        wrapper.gt("submit_time", TimeUtil.getYesterdayStart());
        wrapper.le("submit_time", TimeUtil.getTodayStart());
        List<Response> responses = responseMapper.selectList(wrapper);
        responses.forEach(response -> userSet.add(response.getUserId()));
        //2.2昨日使用AI对话的人
        QueryWrapper<ChatConversation> conversationQueryWrapper = new QueryWrapper<>();
        conversationQueryWrapper.gt("update_time", TimeUtil.getYesterdayStart());
        conversationQueryWrapper.le("update_time", TimeUtil.getTodayStart());
        List<ChatConversation> conversations = conversationMapper.selectList(conversationQueryWrapper);
        conversations.forEach(conversation -> userSet.add(conversation.getUserId()));
        res.setYesActive(userSet.size());
        //3.参与答题的人数
        responses = responseMapper.selectList(new QueryWrapper<>());
        userSet.clear();
        responses.forEach(response -> userSet.add(response.getUserId()));
        res.setAnsQuesUsers(userSet.size());
        //4.recordNums 答题记录总数
        Long record = responseDetailsMapper.selectCount(new QueryWrapper<>());
        res.setRecordNums(record.intValue());
        //5.AIUserNums ai使用人数
        List<ChatConversation> chatConversations = conversationMapper.selectList(new QueryWrapper<>());
        userSet.clear();
        chatConversations.forEach(cc -> userSet.add(cc.getUserId()));
        res.setAIUserNums(userSet.size());
        res.setQuesNums(2);

        return res;
    }

    @Override
    public List<LatestActivityVo> latestActivities() {
        List<LatestActivityVo> res = new ArrayList<>();
        //查询最近活动
        //1.普通用户的创建
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("permission",1);
        userQueryWrapper.select("name","update_time");
        List<User> users = userMapper.selectList(userQueryWrapper);
        users.forEach(user -> res.add(new LatestActivityVo(user.getName()+" 注册/更新了账号",user.getUpdateTime())));
        //2.查询用户答题提交情况
        List<UserResponseVo> userResponseVos = responseMapper.selectUserResponses();
        userResponseVos.forEach(userResponseVo -> res.add(
                new LatestActivityVo(userResponseVo.getName()+" 提交了"+userResponseVo.getType()+"问卷",userResponseVo.getSubmitTime() ))
        );
        //3.聊天记录
        List<UserChatMessageVo> userChatMessageVos = chatMessageMapper.selectChatMessage();
        for (UserChatMessageVo userChatMessageVo : userChatMessageVos) {
            String role = userChatMessageVo.getRole();
            String name = userChatMessageVo.getName();
            LocalDateTime createTime = userChatMessageVo.getCreateTime();
            if ("user".equals(role)){
                res.add(new LatestActivityVo(name+"向AI小助手发起了提问",createTime));
            }else {
                res.add(new LatestActivityVo("AI助手回答了"+name+"的问题",createTime));
            }
        }
        //对结果按照时间降序排序
        res.sort((o1, o2) -> o2.getTime().compareTo(o1.getTime()));
        return res;
    }
}
